package com.ezjie.ielts.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.view.ViewGroup.LayoutParams;


public class CustomDialog extends Dialog { 
	
    private static int default_width = LayoutParams.FILL_PARENT; 
    private static int default_height = LayoutParams.WRAP_CONTENT;
    public CustomDialog(Context context, int layout, int style) { 
        this(context, default_width, default_height, layout, style); 
    }

    public CustomDialog(Context context, int width, int height, int layout, int style) {
        super(context, style);
        //set content
       setContentView(layout);
        //set window params
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        //set width,height by density and gravity
        float density = getDensity(context);
        //代码修改，FILL_PARENT也会留出一个边
        if(width < 0 ) {
        	int[] widthAndHeight = getSrceenPixels(context);
        	
        	params.width = (int) (widthAndHeight[0] - 10 * density);
        }else {
        	params.width = (int) (width*density);
        	
        }
        if(height < 0 ) {
        	params.height = default_height;
        }else {
        	params.height = (int) (height*density);
        	
        }
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);
    }
    private float getDensity(Context context) {
        Resources resources = context.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        return dm.density;
    }
    
    private int[] getSrceenPixels(Context context) {
		DisplayMetrics displaysMetrics = new DisplayMetrics();
		WindowManager windowManager = (WindowManager)(context.getSystemService(Context.WINDOW_SERVICE));
		windowManager.getDefaultDisplay().getMetrics(displaysMetrics);
		int[] widthAndHeight = new int[2];
		widthAndHeight[0] = displaysMetrics.widthPixels;
		widthAndHeight[1] = displaysMetrics.heightPixels;
		return widthAndHeight;
	}
}
